package strcy.cn.market.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import strcy.cn.market.mapper.ProductCategoryMapper;
import strcy.cn.market.pojo.ProductCategories;
import strcy.cn.market.service.ProductCategoryService;
import strcy.cn.market.utils.PageBean;

import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public void add(ProductCategories productCategories) {
        productCategoryMapper.add(productCategories);
    }

    @Override
    public void update(ProductCategories productCategories) {
        productCategoryMapper.update(productCategories);
    }

    @Override
    public void deleteByID(Integer id) {
        productCategoryMapper.deleteById(id);
    }

    @Override
    public List<ProductCategories> list() {
        List<ProductCategories> list = productCategoryMapper.list();
        return list;
    }

    @Override
    public PageBean<ProductCategories> pageList(Integer pageNum, Integer pageSize) {
        //创建pagebean对象
        PageBean<ProductCategories> pb = new PageBean<>();
        //开启分页查询PageHelper
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper
        List<ProductCategories> as =productCategoryMapper.list();
        //Page中提供了方法，可以获取PageHelper分页查询后，得到的总记录数和当前页数据
        Page<ProductCategories> p = (Page<ProductCategories>) as;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
}
